package vn.com.vega.music.view.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.music.objects.Ringtone;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter.AutoLoadingInterface;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RingtoneListAdapter extends BaseAdapter implements Const,
		AutoLoadingInterface {
	private LayoutInflater mInflater;
	private List<Object> mListRingtone = new ArrayList<Object>();
	private LinearLayout mCurrentMenuShow = null;
	private RegisterRingtoneInterface register;

	public RingtoneListAdapter(Activity context, List<Object> objects, RegisterRingtoneInterface register) {
		// mImageLoader = ImageLoader.getInstance(context);
		mInflater = LayoutInflater.from(context);
		if (objects != null) {
			mListRingtone = objects;
		}
		this.register = register;
	}

	@Override
	public int getCount() {
		return mListRingtone.size();
	}

	@Override
	public Object getItem(int position) {
		return mListRingtone.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		Object obj = mListRingtone.get(position);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.ringtone_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView
					.findViewById(R.id.tv_ringtone_title);
			holder.artist = (TextView) convertView
					.findViewById(R.id.tv_ringtone_artist);
			holder.price = (TextView) convertView
					.findViewById(R.id.tv_ringtone_price);
			holder.layoutShowHideMenu = (LinearLayout) convertView
					.findViewById(R.id.layout_show_hide_menu_ringtone_item);
			holder.LayoutMenu = (LinearLayout) convertView
					.findViewById(R.id.layout_menus_ringtone_item);
			holder.registerRingtone = (ImageButton) convertView.findViewById(R.id.bt_ringtone_register);
			holder.presentRingtone = (ImageButton) convertView.findViewById(R.id.bt_ringtone_present);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.LayoutMenu.setVisibility(View.GONE);
		holder.layoutShowHideMenu
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (mCurrentMenuShow == holder.LayoutMenu) {
							if (holder.LayoutMenu.isShown()) {
								holder.LayoutMenu.setVisibility(View.GONE);
							} else {
								holder.LayoutMenu.setVisibility(View.VISIBLE);
							}
						} else {
							if (mCurrentMenuShow != null) {
								mCurrentMenuShow.setVisibility(View.GONE);
							}
							holder.LayoutMenu.setVisibility(View.VISIBLE);
							mCurrentMenuShow = holder.LayoutMenu;
						}
					}
				});

		final Ringtone ringtone = (Ringtone) obj;
		holder.title.setText(ringtone.title);
		holder.artist.setText(ringtone.artist);
		holder.price.setText(ringtone.price);
		
		holder.registerRingtone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mCurrentMenuShow != null) {
					mCurrentMenuShow.setVisibility(View.GONE);
				}
				if (register != null) {
					register.registerRintone(ringtone);
				}
			}
		});
		holder.presentRingtone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mCurrentMenuShow != null) {
					mCurrentMenuShow.setVisibility(View.GONE);
				}
				if (register != null) {
					register.presentRingtone(ringtone);
				}
			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView title;
		TextView artist;
		TextView price;
		LinearLayout layoutShowHideMenu;
		LinearLayout LayoutMenu;
		ImageButton registerRingtone;
		ImageButton presentRingtone;

		// TODO:
		// list other here (menus item,....)
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged) {
		notifyListObjectChanged(objectsChanged, TYPE_LIST_ADDED);
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged, int type) {
		if (objectsChanged != null) {
			if (type == TYPE_LIST_REPLACED) {
				mListRingtone.clear();
			}
			mListRingtone.addAll(objectsChanged);
			notifyDataSetChanged();
		}
	}
	
	public interface RegisterRingtoneInterface{
		public void registerRintone(Ringtone ring);
		public void presentRingtone(Ringtone ring);
	}
}
